<?php 
namespace app\api\controller;

use app\api\ApiController;
use common\service\AuthService;
use common\service\BlockWalletService;
use common\service\UserService;
use common\service\WalletService;

class Wallet extends ApiController
{
    public $steamInfo = [];//用户steam信息

    public function __construct()
    {
        parent::__construct();
        $this->steamInfo = AuthService::getInstance()->verifyToken();
        $this->steamInfo = empty($this->steamInfo) ? [] : $this->steamInfo;
    }

    public function bindWallet()
    {
        if(empty($this->steamInfo)){
            return $this->errorMessage(BAD_ACCESS_TOKEN_NO, BAD_ACCESS_TOKEN_ERROR);
        }
        $params['user'] = $this->steamInfo['access_token'];
        $result = BlockWalletService::getInstance()->getQRcode($params);
        return $this->successJson($result);
    }

    public function notify($msg = null)
    {
        if(empty($msg)){
            return $this->errorMessage(BAD_PARAM_NO, BAD_PARAM_ERROR);
        }

        $ret = BlockWalletService::getInstance()->check($msg);
        if(!$ret){
            return $this->errorMessage(SIGN_FAIL_NO, SIGN_FAIL_ERROR);
        }
        $this->steamInfo = AuthService::getInstance()->verifyToken($ret['user']);
        if(empty($this->steamInfo)){
            return $this->errorMessage(BAD_ACCESS_TOKEN_NO, BAD_ACCESS_TOKEN_ERROR);
        }
        $userId = $this->steamInfo['user_id'];
        $walletAddress = $ret['wallet_address'];
        $result = UserService::getInstance()->saveWalletAddress($userId, $walletAddress);
        if($result){
            return $this->successJson();
        }else{
            return $this->errorMessage(REQUEST_NO, REQUEST_ERROR);
        }
    }

    public function lists($appId = 730, $page = 1, $limit = 25)
    {
        $steamInfo = AuthService::getInstance()->verifyToken();
        if(empty($steamInfo)){
            return $this->errorMessage(BAD_ACCESS_TOKEN_NO, BAD_ACCESS_TOKEN_ERROR);
        }
        $userId = $steamInfo['user_id'];

        $result = $this->validate(
            [
                'appId' => $appId,
                'page'  => $page,
                'limit' => $limit,
            ],
            [
                'appId' => 'number',
                'page'  => 'number',
                'limit' => 'number',
            ]
        );

        if (true !== $result) {
            return $this->errorParams($result);
        }

        $list = WalletService::getInstance()->walletLists($userId, $appId, $page, $limit);

        return $this->successJson($list);
    }

    public function from($appId = 730, $botInvIds = NULL)
    {
        $steamInfo = AuthService::getInstance()->verifyToken();
        if(empty($steamInfo)){
            return $this->errorMessage(BAD_ACCESS_TOKEN_NO, BAD_ACCESS_TOKEN_ERROR);
        }
        $userId = $steamInfo['user_id'];

        $result = $this->validate(
            [
                'appId' => $appId,
                'botInvIds' => $botInvIds,
            ],
            [
                'appId' => 'number',
                'botInvIds' => 'require'
            ]
        );

        if(true !== $result){
            return $this->errorParams($result);
        }
        $botInvIds = explode(',', $botInvIds);

        $result = WalletService::getInstance()->walletFrom($userId, $appId, $botInvIds);
        if ($result == -1) {
            return $this->errorMessage(CUSTOMER_NO, '建议单次存入物品不要超过200个');
        } elseif ($result == -2) {
            return $this->errorMessage(CUSTOMER_NO, '用户不存在');
        } elseif ($result == -3) {
            return $this->errorMessage(CUSTOMER_NO, '请选择未发行物品存入钱包');
        } elseif ($result == -4) {
            return $this->errorMessage(CUSTOMER_NO, '请绑定钱包地址');
        }

        return $this->successJson();
    }

    public function out($appId = 730, $botInvIds = NULL)
    {
        $steamInfo = AuthService::getInstance()->verifyToken();
        if(empty($steamInfo)){
            return $this->errorMessage(BAD_ACCESS_TOKEN_NO, BAD_ACCESS_TOKEN_ERROR);
        }
        $userId = $steamInfo['user_id'];

        $result = $this->validate(
            [
                'appId' => $appId,
                'botInvIds' => $botInvIds,
            ],
            [
                'appId' => 'number',
                'botInvIds' => 'require'
            ]
        );

        if(true !== $result){
            return $this->errorParams($result);
        }
        $botInvIds = explode(',', $botInvIds);

        $result = WalletService::getInstance()->walletOut($userId, $appId, $botInvIds);
        if ($result == -1) {
            return $this->errorMessage(CUSTOMER_NO, '建议单次存入物品不要超过200个');
        } elseif ($result == -2) {
            return $this->errorMessage(CUSTOMER_NO, '用户不存在');
        } elseif ($result == -3) {
            return $this->errorMessage(CUSTOMER_NO, '请选择已发行物品取回');
        } elseif ($result == -4) {
            return $this->errorMessage(CUSTOMER_NO, '请绑定钱包地址');
        }

        return $this->successJson();
    }
}